package demo4;

import demo2.TestDemo1;

public class TestDemo {
    //判断二叉树是不是满二叉树
    //是否是平衡二叉树
    static class TreeNode{
        public int val;
        public TreeNode left;
        public TreeNode right;

        public TreeNode(int val){
            this.val = val;
        }
    }

    //信息体 :
    //满足 : 左树是否是平衡二叉树
    //右树是否是平衡二叉树
    //左树和右树的高度差的绝对值是否<=1

    static class Info{
        public boolean isBalance;
        public int height;

        public Info(boolean isBalance,int height){
            this.isBalance = isBalance;
            this.height = height;
        }
    }

    public static Info process(TreeNode head){
        if(head==null) return new Info(true,0);
        Info leftInfo = process(head.left);
        Info rightInfo = process(head.right);
        int height = Math.max(leftInfo.height, rightInfo.height) +1;
        boolean isBalance = true;
        if(!leftInfo.isBalance){
            isBalance = false;
        }
        if(!rightInfo.isBalance){
            isBalance = false;
        }
        if(Math.abs(leftInfo.height- rightInfo.height)>1){
            isBalance = false;
        }

        return new Info(isBalance,height);
    }

    public static boolean isBalanceTree(TreeNode head){
        return process(head).isBalance;
    }



    public static void main(String[] args) {

    }


}
